//
//  ExpenseCell.m
//  iMoney
//
//  Created by VertigoSol on 9/24/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "ExpenseCell.h"


@implementation ExpenseCell
@synthesize name, date, amount, type, dateFormatter, year;

-(void)setVals:(NSString*)nname: (NSString*) ntype: (float)namount: (NSDate*) ndate;
{
	
	
	if(namount == 0)
	{
		amount.textColor = [UIColor whiteColor];
		amount.text = [NSString stringWithFormat:@"%.2f", namount];
	}
	else if(namount > 0)
	{
		amount.textColor = [UIColor blueColor];	
		amount.text = [NSString stringWithFormat:@"%.2f", namount];
	}
	else {
		amount.textColor = [UIColor redColor];
		amount.text = [NSString stringWithFormat:@"(%.2f)", namount];
	}

		
	
	
	//dateFormatter = [[[NSDateFormatter alloc]
//									   initWithDateFormat:@"%1m/%1d/%Y" allowNaturalLanguage:NO] autorelease];
	
	
	dateFormatter = [[[NSDateFormatter alloc]
									   init] autorelease];
	
	[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
	[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
	
	NSArray *split = [[dateFormatter stringFromDate:ndate] componentsSeparatedByString:@","];
	
	name.text = [nname copy];
	type.text = [ntype copy];
	date.text = [[split objectAtIndex:0] copy];
	year.text = [[split objectAtIndex:1] copy];
}



- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
	
	if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {		
		
		UIView *myContentView = self.contentView;
		
		self.name = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:14.0 bold:YES]; 
		self.name.textAlignment = UITextAlignmentLeft; // default
		[myContentView addSubview:self.name];
		[self.name release];
		
		self.type = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:12.0 bold:NO]; 
		self.type.textAlignment = UITextAlignmentLeft; // default
		[myContentView addSubview:self.type];
		[self.type release];
		
		self.amount = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:14.0 bold:YES]; 
		self.amount.textAlignment = UITextAlignmentLeft; // default
		[myContentView addSubview:self.amount];
		[self.amount release];
		
		self.date = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:14.0 bold:YES]; 
		self.date.textAlignment = UITextAlignmentLeft; // default
		[myContentView addSubview:self.date];
		[self.date release];
		
		self.year = [self newLabelWithPrimaryColor: [UIColor lightTextColor] selectedColor:[UIColor whiteColor] fontSize:12.0 bold:NO]; 
		self.year.textAlignment = UITextAlignmentLeft; // default
		[myContentView addSubview:self.year];
		[self.year release];
		
		isAddRow = NO;
		
	}
	
	return self;
	
	
}

-(void) makeAddRow:(BOOL)yes
{
	isAddRow = yes;
	
}


- (void)layoutSubviews {
	
    [super layoutSubviews];
	
	// getting the cell size
    CGRect contentRect = self.contentView.bounds;
	// get the X pixel spot
	CGFloat boundsX = contentRect.origin.x;
	CGRect frame;
	
	
	if(isAddRow) //display add row
	{

		frame = CGRectZero;
		self.date.frame = frame;
		self.amount.frame = frame;
		self.type.frame = frame;
		self.year.frame = frame;
		
		self.name.text = @"Add New Expense";
		frame = CGRectMake(boundsX + 10, 12, 200, 20);
		self.name.frame = frame;
		
		return;
	}
	
    if (!self.editing) {
	
        /*
		 Place the title label.
		 place the label whatever the current X is plus 10 pixels from the left
		 place the label 4 pixels from the top
		 make the label 200 pixels wide
		 make the label 20 pixels high
		 */
		
		frame = CGRectMake(boundsX + 80, 4, 120, 20);
		self.name.frame = frame;
        
		frame = CGRectMake(boundsX + 80, 28, 120, 12);
		self.type.frame = frame;
		
		frame = CGRectMake(boundsX + 10, 4, 50, 20);
		self.date.frame = frame;
		
		frame = CGRectMake(boundsX + 10, 28, 50, 12);
		self.year.frame = frame;
		
		frame = CGRectMake(boundsX + 195, 4, 100, 30);
		self.amount.frame = frame;
		
	}
	else
	{
		//frame = CGRectMake(boundsX + 10, 4, 120, 20);
//		self.name.frame = frame;
//        
//		frame = CGRectMake(boundsX + 10, 28, 120, 12);
//		self.type.frame = frame;
//		
//		frame = CGRectZero;
//		self.date.frame = frame;
		
		frame = CGRectMake(boundsX + 80, 4, 120, 20);
		self.name.frame = frame;
        
		frame = CGRectMake(boundsX + 80, 28, 120, 12);
		self.type.frame = frame;
		
		frame = CGRectMake(boundsX + 10, 2, 50, 30);
		self.date.frame = frame;
	
		frame = CGRectMake(boundsX + 10, 28, 50, 12);
		self.year.frame = frame;
		
		frame = CGRectZero;
		self.amount.frame = frame;
		
	}
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
	
    [super setSelected:selected animated:animated];
	
    // Configure the view for the selected state
}

- (UILabel *)newLabelWithPrimaryColor:(UIColor *)primaryColor selectedColor:(UIColor *)selectedColor fontSize:(CGFloat)fontSize bold:(BOOL)bold
{
	/*
	 Create and configure a label.
	 */
	
    UIFont *font;
    if (bold) {
        font = [UIFont boldSystemFontOfSize:fontSize];
    } else {
        font = [UIFont systemFontOfSize:fontSize];
    }
    
    /*
	 Views are drawn most efficiently when they are opaque and do not have a clear background, so set these defaults.  To show selection properly, however, the views need to be transparent (so that the selection color shows through).  This is handled in setSelected:animated:.
	 */
	UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectZero];
	newLabel.backgroundColor = [UIColor clearColor];
	newLabel.opaque = NO;
	newLabel.textColor = primaryColor;
	newLabel.highlightedTextColor = selectedColor;
	newLabel.font = font;
	
	return newLabel;
}



- (void)dealloc {
	[year release];
	[name release];
	[type release];
	[amount release];
	//[date release];
    [super dealloc];
}


@end
